package com.getpebble.android.util;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build.VERSION;
import android.os.Bundle;
import android.webkit.WebView;
import com.getpebble.android.PebbleApplication;
import com.getpebble.android.common.core.trace.Analytics.RemoteAppBehavior;
import com.getpebble.android.common.core.trace.Analytics.Support;
import com.getpebble.android.common.core.trace.AndroidLogger;
import com.getpebble.android.common.core.trace.Trace;
import com.getpebble.android.common.core.util.PblPreferences;
import com.getpebble.android.common.core.util.PblPreferences.PrefKey;
import com.getpebble.android.common.model.PblDeviceModel.PblDeviceRecord;
import com.getpebble.android.config.SyncBootConfig;
import com.getpebble.android.core.analytics.AnalyticsSetup;
import com.getpebble.android.main.fragment.FragmentMetaData.FragmentType;
import com.getpebble.android.main.sections.appstore.constants.AppStoreConstants.StoreType;
import com.getpebble.android.onboarding.OnboardingPreferences;
import java.util.List;

public class DeepLink {
    public static final String TAG = DeepLink.class.getSimpleName();
    private Context mContext;
    private String mDeepLinkType;
    private boolean mForceSkipOnboarding;
    private Bundle mFragmentArgs;
    private FragmentType mFragmentType;
    private Intent mIntent;
    private Uri mUri;

    public DeepLink(Intent intent, Context context) {
        this.mIntent = intent;
        this.mContext = context;
        parseIntent();
        Trace.verbose(TAG, "DeepLink() mDeepLinkType = " + this.mDeepLinkType);
    }

    private void parseIntent() {
        if (this.mIntent != null) {
            this.mUri = this.mIntent.getData();
            if (this.mUri != null && "pebble".equals(this.mUri.getScheme())) {
                this.mDeepLinkType = this.mUri.getHost();
                if (this.mUri.getHost().equals("support-email") || this.mUri.getHost().equals("support-email-nologs") || this.mUri.getHost().equals("skip-onboarding")) {
                    this.mForceSkipOnboarding = true;
                }
            }
        }
    }

    public void handleLink() {
        if (isDeepLink() && !needsFragmentSwitch()) {
            if (this.mDeepLinkType.equals("custom-boot-config-url")) {
                String bootConfigUrl = this.mUri.getPath().replaceFirst("/", "");
                Trace.warning(TAG, "Setting BootConfig url to " + bootConfigUrl);
                PebbleApplication.getSessionManager().removeAccount();
                SyncBootConfig.syncBootConfigIfRequired(bootConfigUrl, this.mContext, new Runnable() {
                    public void run() {
                        AnalyticsSetup.requestReinitializeMobileAnalytics();
                    }
                }, true);
            } else if (this.mDeepLinkType.equals("enable-verbose-logging")) {
                AndroidLogger.setVerboseLogcat(true);
            } else if (this.mDeepLinkType.equals("skip-onboarding")) {
                PblDeviceRecord lastConnectedDevice = PebbleApplication.getLastConnectedDeviceRecord();
                if (lastConnectedDevice != null) {
                    OnboardingPreferences.setOnboardingVersion(lastConnectedDevice.pblDevice, 1);
                } else {
                    Trace.warning(TAG, "No last connected device; cannot skip onboarding");
                }
            } else if (this.mDeepLinkType.equals("hex-dump")) {
                String onOff = this.mUri.getPath();
                PblPreferences prefs = new PblPreferences(PebbleApplication.getAppContext());
                if ("/on".equals(onOff)) {
                    Trace.warning(TAG, "Setting hex-dump to enabled");
                    prefs.setBooleanData(PrefKey.HEX_DUMP, true);
                } else if ("/off".equals(onOff)) {
                    Trace.warning(TAG, "Setting hex-dump to disabled");
                    prefs.setBooleanData(PrefKey.HEX_DUMP, false);
                } else {
                    Trace.warning(TAG, "Unknown param for HEX_DUMP: '" + onOff + "'");
                }
            } else if (!this.mDeepLinkType.equals("webview-debug")) {
            } else {
                if (VERSION.SDK_INT >= 19) {
                    Trace.warning(TAG, "Enabling WebView debugging from deep link");
                    WebView.setWebContentsDebuggingEnabled(true);
                    return;
                }
                Trace.warning(TAG, "Could not enable WebView debugging from deep link - not on KitKat");
            }
        }
    }

    public boolean isDeepLink() {
        return this.mDeepLinkType != null;
    }

    public boolean needsFragmentSwitch() {
        if (!isDeepLink()) {
            return false;
        }
        if (this.mDeepLinkType.equals("appstore") || this.mDeepLinkType.equals("support-email") || this.mDeepLinkType.equals("support-email-nologs")) {
            return true;
        }
        return false;
    }

    public FragmentType getFragmentTypeToSwitch() {
        if (this.mFragmentType == null) {
            loadFragmentData();
        }
        return this.mFragmentType;
    }

    public Bundle getFragmentArgs() {
        if (this.mFragmentArgs == null) {
            loadFragmentData();
        }
        return this.mFragmentArgs;
    }

    private void loadFragmentData() {
        if (needsFragmentSwitch()) {
            if (this.mDeepLinkType.equals("appstore")) {
                List<String> extraData = this.mUri.getPathSegments();
                if (extraData.size() < 1) {
                    Trace.warning(TAG, "No appstore app_id found.");
                    return;
                }
                String appStoreId = (String) extraData.get(0);
                this.mFragmentArgs = new Bundle();
                this.mFragmentArgs.putInt("extra_store_type", StoreType.APPLICATION.ordinal());
                this.mFragmentArgs.putString("extra_page_id", appStoreId);
                this.mFragmentType = FragmentType.APP_STORE_APPLICATION;
                RemoteAppBehavior.logUrlReceivedAppStorePage(appStoreId);
            }
            if (this.mDeepLinkType.equals("support-email") || this.mDeepLinkType.equals("support-email-nologs")) {
                boolean includeLogs = this.mDeepLinkType.equals("support-email");
                this.mFragmentArgs = new Bundle();
                this.mFragmentArgs.putBoolean("support_email_request", true);
                this.mFragmentArgs.putBoolean("include_logs", includeLogs);
                this.mFragmentType = FragmentType.SUPPORT;
                if (includeLogs) {
                    Support.logUrlReceivedSupportEmail();
                } else {
                    Support.logUrlReceivedSupportEmailNoLogs();
                }
            }
        }
    }

    public boolean forceSkipOnboarding() {
        return this.mForceSkipOnboarding;
    }
}
